package com.atguigu.edu.service.impl;

import com.atguigu.edu.client.VodClient;
import com.atguigu.edu.entity.Video;
import com.atguigu.edu.mapper.VideoMapper;
import com.atguigu.edu.service.VideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-08-10
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    @Autowired
    private VodClient vodClient;
    @Override
    public void deleteVideoByCourseId(String courseId) {
        QueryWrapper<Video> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id",courseId);
        videoWrapper.select("video_source_id");
        List<Video> videoList = baseMapper.selectList(videoWrapper);
        List<String> videoIdList = new ArrayList<>();
        if(videoList.size() >0) {
            for (int i = 0; i < videoList.size(); i++) {
                Video video = videoList.get(i);
                if(!StringUtils.isEmpty(video.getVideoSourceId())) {
                    videoIdList.add(video.getVideoSourceId());
                }
            }
        }
        if (videoIdList.size() >0)  {
            vodClient.deleteBatch(videoIdList);
        }
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        baseMapper.delete(queryWrapper);
    }
}
